<?php
/*------------------------------------------------------------------------------
this file is part of Web Application Kit http://webappkit.net
Copyright (C) 2006 J.Ducastel fredbird@fredbird.org

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
------------------------------------------------------------------------------*/

/**
* manipulate url as an object
* @author Fred Bird <fredbird@fredbird.org>
* @version 
*/
class urlBean extends dataBean {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	*
	* @access
	*/
	function urlBean($url=NULL,$base=NULL) {
		$this->__construct($url,$base);
	}/**/
	
	function __construct($url=NULL,$base=NULL) {
		$parts=$this->parseString($url);
		if ($parts) {
			$this->setMember('scheme',$parts['scheme']);
			$this->setMember('host',$parts['host']);
			$this->setMember('path',$parts['path']);
			$this->setMember('vars',$parts['vars']);
			if ($parts['fragment'])
				$this->setMember('fragment',$parts['fragment']);
		}
		if ($base)
			$this->setbasepath($base);
		//echo wakapi::vardump($this);
	}
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* parse an url string
	* @access public
	* @param string $string url as a string
	* @static
	* @return array parsed string
	*/
	function parseString($string=NULL) {
		
		$data=parse_url($string);
		//echo wakapi::vardump($data);
		// $data['protocol']=$data['scheme'];
		$data['scheme']=isset($data['scheme'])?$data['scheme']:null;
		$data['host']=isset($data['host'])?$data['host']:null;
		// parsing query
		$getVars=isset($data['query'])?explode('&',$data['query']):array();
		$data['vars']=array();
		foreach ($getVars as $var) {
			if (!$var) continue;
			$var=explode('=',$var);
			$data['vars'][$var[0]]=$var[1];
		}
		// parsing fragment
		// ereg('#(.+)$',$string,$search); echo wakaffvar($search);
		if (ereg('#(.+)$',$string,$search))
			$data['fragment']=$search[1];
		else $data['fragment']=null;
		// echo wakaffvar($data);
		return $data;
	}/**/
	
	/**
	* returns the relative url (without base)
	* @access public
	* @return string
	*/
	function getRelative() {
		$path=$this->getPath();
		$query=$this->getquery();
		$fragment=$this->getfragment();
		return $path
		.($query?'?'.$query:'')
		.($fragment?'#'.$fragment:'');
	}/**/
	
	/**
	* returns the full absolute url
	* @access public
	* @return string
	*/
	function getAbsolute() {
		$scheme=$this->getscheme();
		$host=$this->getHost();
		$basepath=$this->getBasePath();
		$path=$this->getPath();
		$query=$this->getquery();
		$fragment=$this->getfragment();
		return $scheme.'://'
		.$host.'/'
		.($basePath?$basepath.'/':'')
		.$path
		.($query?'?'.$query:'')
		.($fragment?'#'.$fragment:'');
	}/**/
	
	/**
	* returns the query part (without ?)
	* @access public
	* @return string
	*/
	function getQuery() {
		$query='';
		foreach($this->vars as $key => $value)
			$query.="$key=$value&amp;";
		// wiping last unnecessary &amp;
		$query=substr($query,0,-5);
		return $query;
	}/**/
	
	/**
	* return current $scheme value
	* @access public
	* @return string current $scheme
	*/
	function getScheme($scheme=null) {
		return $this->getMember('scheme');
	}/**/
	
	/**
	* set $scheme value
	* @access public
	* @return string current $scheme
	*/
	function setScheme($scheme=null) {
		return $this->setMember('scheme',$scheme);
	}/**/
	
	/**
	* return current $host value, or set a new one
	* @access public
	* @return string current $host
	*/
	function gethost() {
		return $this->getMember('host');
	}/**/
	
	/**
	* set $host value
	* @access public
	* @return string current $host
	*/
	function sethost($host=null) {
		return $this->setMember('host',$host);
	}/**/
	
	/**
	* return current $path value, or set a new one
	* @access public
	* @return string current $path
	*/
	function getpath() {
		return $this->getMember('path');
	}/**/
	
	/**
	* return current $path value, or set a new one
	* @access public
	* @return string current $path
	*/
	function setpath($path) {
		return $this->setMember('path',$path);
	}/**/
	
	/**
	* return $index segment of uri
	* i.e. part after $index slashes and before next slash
	* @access public
	* @param int $index
	* @return string|null
	*/
	function getUriSegment($index) {
		if (!$path=$this->getMember('path'))
			return null;
		$segments=explode('/',$path);
		return isset($segments[$index])?$segments[$index]:null;
	}/**/
	
	/**
	* return current $basepath value
	* @access public
	* @return bool
	*/
	function getbasePath() {
		return $this->getMember('basepath');
	}/**/
	
	/**
	* return current $basepath value, or set a new one
	* @param string $base base url
	* @param bool $adjustPath
	* @access public
	* @return bool
	*/
	function setbasePath($basepath=null,$adjustPath=true) {
		if (!$parsed=parse_url($basepath))
			return false;
		$basepath=$parsed['path']; //echo "<p>$basepath</p>";
		$this->setMember('basepath',$basepath);
		//echo wakapi::vardump($this);
		// adjusting path by wiping the base part
		if ($adjustPath and substr($this->path,0,strlen($this->basepath))==$this->basepath) {
			$path=substr($this->path,strlen($this->basepath));
			// echo "<p>adjusting path to $path</p>";
			$this->setMember('path',$path);
		}
		//echo wakapi::vardump($this);
		return true;
	}/**/
	
	/**
	* return current $vars[$var] value, or set a new one
	* @access public
	* @return string current $scheme
	*/
	function getvar($key) {
		if (!isset($this->vars[$keys])) return null;
		return $this->vars[$key];
	}/**/
	
	/**
	* return current $vars[$var] value, or set a new one
	* @access public
	* @return string current $scheme
	*/
	function setvar($key, $value) {
		$this->vars[$key]=$value;
		return true;
	}/**/
	
	/**
	* return current $vars value, or set a new one
	* @access public
	* @return string current $scheme
	*/
	function getvars() {
		return $this->get('vars');
	}/**/
	
	/**
	* return current $vars value, or set a new one
	* @access public
	* @return string current $scheme
	*/
	function setvars($vars) {
		return $this->setMember('vars',$vars);
	}/**/
	
	/**
	* return current $fragment value, or set a new one
	* @access public
	* @return string current $scheme
	*/
	function getfragment() {
		return $this->get('fragment');
	}/**/
	/**
	* return current $fragment value, or set a new one
	* @access public
	* @return string current $scheme
	*/
	function fragment($fragment) {
		return $this->setMember('fragment',$fragment);
	}/**/

/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	var $_databean_lock_structure=true;

	/**
	* @var type desc
	*/
	var $scheme;
	function _scheme_check($x) {
		return ereg('^(http)|(https)|(ftp)$',$x);
	}
	/**
	* @var type desc
	*/
	var $host;
	function _host_check($x) {
		return ereg('^[a-zA-Z0-9\.-]+$',$x);
	}
	/**
	* @var string base path (= physical path to index)
	*/
	var $basepath;
	function _basepath_check($x) {
		return ereg('^[a-zA-Z0-9_/\.-]+$',$x);
	}
	function _basepath_inputfilter($basepath) {
		return trim($basepath,'/');
	}
	/**
	* @var string relative path (not starting by /)
	*/
	var $path;
	function _path_check($x) {
		return ereg('^[a-zA-Z0-9_/\.-]*$',$x);
	}
	function _path_inputfilter($path) {
		$newpath=trim($path,'/'); //echo "$path >> $newpath";
		return $newpath;
	}/**/

	/**
	* @var array GET vars
	*/
	var $vars;
	function _vars_check($x) {
		return is_array($x);
	}
	/**
	* @var type desc
	*/
	var $fragment;
	function _fragment_check($x) {
		return is_string($x) and ereg('^[a-zA-Z][a-zA-Z0-9_-]*$',$x);
	}
	function _fragment_inputfilter($fragment) {
		if (is_string($fragment))
			$fragment=str_replace('#','',$fragment);
		return $fragment;
	}
	
	/**
	* @var type desc
	*/
	//var //
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function () {
		
	}/**/
}
?>
